<?php
if (!defined("DNREAD")) exit();
global $db,$basepref,$api,$lang,$setting;
$bc = '';
$bs = array(
            'sort' => array('lang'=>'all_sorting',
                            'form'=>'select',
                            'value'=>array('last'=>'all_last','rand'=>'all_random'),
                            'default'=>'last'),
           );
if (defined('SETTING')) {
    return $bs;
}
if (isset($setting['bsarray']) && is_array($setting['bsarray'])) {
    $bs = $setting['bsarray'];
}
if ($bs['sort'] == 'rand') {
	$inq = $db->query("SELECT pollid,poll_title,poll_decs,poll_ajax,poll_only FROM ".$basepref."_polling WHERE poll_act='yes' AND poll_finish > '".NEWTIME."' ORDER BY MD5(RAND()) LIMIT 1");
} else {
	$inq = $db->query("SELECT pollid,poll_title,poll_decs,poll_ajax,poll_only FROM ".$basepref."_polling WHERE poll_act='yes' AND poll_finish > '".NEWTIME."' ORDER BY pollid ASC LIMIT 1");
}
if ($db->numrows($inq) > 0) {
    $item = $db->fetchrow($inq);
    $ins['view'] = 0;
    $ins['message'] = '';
    if ($item['poll_only'] == 'user') {
        if (preparse($usermain['logged'],THIS_INT) == 1 && preparse($usermain['userid'],THIS_INT) > 0) {
            $vote = $db->fetchrow($db->query("SELECT SUM(voteid) AS total FROM ".$basepref."_polling_vote WHERE pollid='".$item['pollid']."' AND userid='".$usermain['userid']."'"));
            $ins['view'] = ($vote['total'] == 0) ? 1 : 0;
            $ins['message'] = ($vote['total'] > 0) ? $lang['poll_dle'] : '';
        }
    } else {
        $vote = $db->fetchrow($db->query("SELECT SUM(voteid) AS total FROM ".$basepref."_polling_vote WHERE pollid='".$item['pollid']."' AND voteip='".REMOTE_ADDRS."'"));
        $ins['view'] = ($vote['total'] == 0) ? 1 : 0;
        $ins['message'] = ($vote['total'] > 0) ? $lang['poll_dle'] : '';
    }
    $tm->unmanule['ajax'] = ($setting['ajax'] == 'yes' && $item['poll_ajax'] == 'yes') ? 'yes' : 'no';
    $ins['template'] = ($ins['view'] == 0) ? $tm->parsein($tm->create('mod/poll/poll.block.view')) : $tm->parsein($tm->create('mod/poll/poll.block.form'));
    $ins['count'] = $db->fetchrow($db->query("SELECT SUM(vals_voices) AS total FROM ".$basepref."_polling_vals WHERE pollid='".$item['pollid']."'"));
    $inq = $db->query("SELECT * FROM ".$basepref."_polling_vals WHERE pollid='".$item['pollid']."' ORDER BY posit");
    $ins['voices'] = '';
    while ($vitem = $db->fetchrow($inq)) {
        $voices = preparse($vitem['vals_voices'],THIS_INT);
        $percent = ($voices > 0) ? (int)(($voices * 100) / $ins['count']['total']) : $voices;
        $line = ($voices > 0) ? $percent : 1;
        $radio = ($ins['view'] == 1) ? '<input type="radio" name="vid" value="'.$vitem['valsid'].'" />' : '';
        $ins['voices'].= $tm->parse(
        array(
        'val_name'=>$api->siteuni($vitem['vals_title']),
        'radio'=>$radio,
        'val_voc'=>$voices.' '.$lang['poll_vocshort'],
        'val_line'=>$line.'%',
        'val_color'=>'#'.$vitem['vals_color'],
        'val_perc'=>$percent
        ),
        $tm->manuale['percent']);
    }
    $button = ($ins['view'] == 0) ? '' : $lang['poll_button'];
    $voteajax = ($setting['ajax'] == 'yes' && $item['poll_ajax'] == 'yes') ? 1 : 0;
    $votejs = ($voteajax == 1) ? 'true' : 'false';
    $bc.= $tm->parse(
    array(
    'id'=>$item['pollid'],
    'vote_ajax'=>$votejs,
    'val'=>$voteajax,
    'title'=>$api->siteuni($item['poll_title']),
    'percent'=>$ins['voices'],
    'desc'=>$api->siteuni($item['poll_decs']),
    'message'=>$ins['message'],
    'all_sends'=>$lang['all_sends'],
    'button'=>$button
    ),
    $ins['template']);
} else {
	$bc = '<div class="pad ac">'.$lang['not_poll'].'</div>';
}
return $api->siteuni($bc);
?>
